#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "../../src/module/amqp/amqp_comm9.h"
#include "../../src/module/amqp/amqp_core.h"
#include "../../src/module/amqp/config.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"
#include "frlist.h"
#include "frreport_comm.h"
#include "../comm/apmtest_comm.h"
#include "apmtest_cunit.h"


int amqp_basic_publish_0_9_0(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, L7session_amqp_private *p_st_session_private, int i_flag_need);

L7session_amqp_private* __amqp_session_private_create(void);
st_amqp_private_data* __amqp_private_create(void);

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    L7session_amqp_private *p_st_session_private = NULL;
    int i_ret = 0, i_flag_need = FR_YES;
    FILE *fp_file = NULL;
    char *p_data = NULL;
    int i_len = 0;

    fp_file = fopen ("t_amqp_comm9/t_amqp_basic_publish_0_9_0/test1.txt", "rb");
    fseek (fp_file, 0, SEEK_END);

    i_len = ftell (fp_file);

    p_data = (char *)frmalloc (i_len);
    if(p_data == NULL)
        return;

    rewind (fp_file);
    fread (p_data, 1, i_len, fp_file);
    fclose (fp_file);

    p_st_data = bytebuff_creat ();
    bytebuff_add (p_st_data, p_data, i_len);

    i_ret = apmtest_create_ct(&p_st_conn, amqp_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 5672);
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    p_st_session_private = __amqp_session_private_create();

    p_st_session_private->p_st_private = __amqp_private_create();

    i_ret = amqp_basic_publish_0_9_0(p_st_data, p_st_body_data, p_st_session_private, i_flag_need);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    amqp_session_destroy (p_st_session_private);
    frreport_body_free (&p_st_body_data);
    bytebuff_free (&p_st_data);
    frfree (p_data);
    apmtest_destroy_ct(&p_st_conn);
    return;

}

static void __case2(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    L7session_amqp_private *p_st_session_private = NULL;
    int i_ret = 0, i_flag_need = FR_NO;
    FILE *fp_file = NULL;
    char *p_data = NULL;
    int i_len = 0;

    fp_file = fopen ("t_amqp_comm9/t_amqp_basic_publish_0_9_0/test1.txt", "rb");
    fseek (fp_file, 0, SEEK_END);

    i_len = ftell (fp_file);

    p_data = (char *)frmalloc (i_len);
    if(p_data == NULL)
        return;

    rewind (fp_file);
    fread (p_data, 1, i_len, fp_file);
    fclose (fp_file);

    p_st_data = bytebuff_creat ();
    bytebuff_add (p_st_data, p_data, i_len);

    p_st_session_private = __amqp_session_private_create();

    i_ret = amqp_basic_publish_0_9_0(p_st_data, p_st_body_data, p_st_session_private, i_flag_need);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    amqp_session_destroy (p_st_session_private);

    bytebuff_free (&p_st_data);
    frfree (p_data);

    return;

}


static void __case3(void)
{
    Conntrack *p_st_conn = NULL;
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    L7session_amqp_private *p_st_session_private = NULL;
    int i_ret = 0, i_flag_need = FR_NO;
    FILE *fp_file = NULL;
    char *p_data = NULL;
    int i_len = 0;

    fp_file = fopen ("t_amqp_comm9/t_amqp_basic_publish_0_9_0/test2.txt", "rb");
    fseek (fp_file, 0, SEEK_END);

    i_len = ftell (fp_file);

    p_data = (char *)frmalloc (i_len);
    if(p_data == NULL)
        return;

    rewind (fp_file);
    fread (p_data, 1, i_len, fp_file);
    fclose (fp_file);

    p_st_data = bytebuff_creat ();
    bytebuff_add (p_st_data, p_data, i_len);

    i_ret = apmtest_create_ct(&p_st_conn, amqp_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 5672);
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    p_st_session_private = __amqp_session_private_create();

    i_ret = amqp_basic_publish_0_9_0(p_st_data, p_st_body_data, p_st_session_private, i_flag_need);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    amqp_session_destroy (p_st_session_private);
    frreport_body_free (&p_st_body_data);
    bytebuff_free (&p_st_data);
    frfree (p_data);
    apmtest_destroy_ct(&p_st_conn);

    return;

}

static void __case4(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    L7session_amqp_private *p_st_session_private = NULL;
    int i_ret = 0, i_flag_need = FR_NO;
    FILE *fp_file = NULL;
    char *p_data = NULL;
    int i_len = 0;

    fp_file = fopen ("t_amqp_comm9/t_amqp_basic_publish_0_9_0/test2.txt", "rb");
    fseek (fp_file, 0, SEEK_END);

    i_len = ftell (fp_file);

    p_data = (char *)frmalloc (i_len);
    if(p_data == NULL)
        return;

    rewind (fp_file);
    fread (p_data, 1, i_len, fp_file);
    fclose (fp_file);

    p_st_data = bytebuff_creat ();
    bytebuff_add (p_st_data, p_data, i_len);

    p_st_session_private = __amqp_session_private_create();

    i_ret = amqp_basic_publish_0_9_0(p_st_data, p_st_body_data, p_st_session_private, i_flag_need);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    amqp_session_destroy (p_st_session_private);

    bytebuff_free (&p_st_data);
    frfree (p_data);

    return;

}


int add_suite_amqp_basic_publish_0_9_0(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" amqp_basic_publish_0_9_0()", __suite_init, __suite_cleanup)) == NULL)
    return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);

    return FR_SUCC;

}

